#include "mainwindow.h"
#include <QVBoxLayout>
#include <QMessageBox>
#include <QGuiApplication>
#include <QScreen>
#include <QPixmap>
#include <QLabel>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    // 获取主屏幕
    QScreen* screen = QGuiApplication::primaryScreen();
    if (!screen)
    {
        QMessageBox::critical(nullptr, "Error", "Failed to get screen");
        return;
    }

    // 捕获整个屏幕
    QPixmap pixmap = screen->grabWindow(0);
    if (pixmap.isNull())
    {
        QMessageBox::critical(nullptr, "Error", "Failed to capture screen");
        return;
    }
    QString filePath = "/home/chenxi/图片/screenshot.png";
    if (pixmap.save(filePath))
    {
        QMessageBox::information(nullptr, "Success", "Screenshot saved to " + filePath);
    }
    else
    {
        QMessageBox::critical(nullptr, "Error", "Failed to save screenshot");
        qDebug() << "Error saving screenshot" ;
    }
    // 创建 QLabel 并设置图片
    QLabel* label = new QLabel(this);
    label->setPixmap(pixmap);
    label->setScaledContents(true); // 缩放图片以适应窗口

    // 创建布局并添加 QLabel
    QVBoxLayout* layout = new QVBoxLayout();
    layout->addWidget(label);

    // 创建一个 QWidget 作为中央窗口，并设置布局
    QWidget* centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);

    // 设置窗口大小
    resize(800, 600); // 可以根据需要调整窗口大小
}

MainWindow::~MainWindow() {}
